package com.jjwxc.jwjskandriod.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.config.Url;
import com.jjwxc.jwjskandriod.framework.base.FFActivity;
import com.jjwxc.jwjskandriod.framework.http.FFNetWorkCallBack;
import com.jjwxc.jwjskandriod.framework.utils.PreUtils;
import com.jjwxc.jwjskandriod.framework.utils.StUtils;
import com.jjwxc.jwjskandriod.model.ChannelListResponse;
import com.jjwxc.jwjskandriod.model.ListUserBookRequest;

import java.util.ArrayList;
import java.util.List;

public class InterestPop {
    String oneIds = "";
    String twoIds = "";
    String threeIds = "";
    String fourIds = "";
    List<String> tagsId = new ArrayList<>();
    ListUserBookRequest bookRequest = new ListUserBookRequest();

    public void interestPopWindow(View mView, Activity context, PopInterestListener listener) {


        View view = LayoutInflater.from(context).inflate(R.layout.interest_pop, null, false);

        TagGroupView tgv_one = view.findViewById(R.id.tgv_one);

        TagGroupView tgv_two = view.findViewById(R.id.tgv_two);
        TextView tv_two = view.findViewById(R.id.tv_two);

        TagGroupView tgv_three = view.findViewById(R.id.tgv_three);
        TextView tv_three = view.findViewById(R.id.tv_three);

        TagGroupView tgv_four = view.findViewById(R.id.tgv_four);
        TextView tv_four = view.findViewById(R.id.tv_four);
        LinearLayout ll_quxiao = view.findViewById(R.id.ll_quxiao);
        TextView tv_tab_one = view.findViewById(R.id.tv_tab_one);
        TextView tv_tab_two = view.findViewById(R.id.tv_tab_two);
        TextView tv_ok = view.findViewById(R.id.tv_ok);
        TextView tv_quxiao = view.findViewById(R.id.tv_quxiao);
        TextView tv_time = view.findViewById(R.id.tv_time);
        LinearLayout ll_time = view.findViewById(R.id.ll_time);


        //1.构造一个PopupWindow，参数依次是加载的 View，宽高
        final PopupWindow popWindow = new PopupWindow(view,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        //设置加载动画
        popWindow.setAnimationStyle(R.style.PopupWindow);
        //这些为了点击非PopupWindow区域，PopupWindow会消失的，如果没有下面的
        //代码的话，你会发现，当你把PopupWindow显示出来了，无论你按多少次后退键
        //PopupWindow并不会关闭，而且退不出程序，加上下述代码可以解决这个问题

        popWindow.setTouchable(true);
        popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(1, context);
            }
        });

        popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));    //要为popWindow设置一个背景才有效
        //设置 popupWindow 显示的位置，参数依次是参照 View，x轴的偏移量，y轴的偏移量
        popWindow.showAtLocation(mView, Gravity.BOTTOM, 0, 0);

        setBackgroundAlpha(0.3f, context);

        ll_quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });
        String bookr = PreUtils.getString("Interest_Hobby", null);
        if (!StUtils.isEmpty(bookr)) {
            bookRequest = JSON.parseObject(bookr, ListUserBookRequest.class);
            oneIds = bookRequest.getTab();
            if (bookRequest.getChannels() != null && bookRequest.getChannels().size() > 0) {
                twoIds = bookRequest.getChannels().get(0);
            }
            threeIds = bookRequest.getWordSize();
            fourIds = bookRequest.getPublishTime();
            if (bookRequest.getTagIds() != null && bookRequest.getTagIds().size() > 0) {
                tagsId = bookRequest.getTagIds();
                tv_tab_one.setBackgroundResource(R.drawable.tag_screen_select_false);
                tv_tab_one.setTextColor(Color.parseColor("#222222"));
                tv_tab_two.setText("已选" + tagsId.size() + "个");
                tv_tab_two.setBackgroundResource(R.drawable.item_tab_text_true);
                tv_tab_two.setTextColor(Color.parseColor("#ffffff"));
            }else{
                tv_tab_two.setBackgroundResource(R.drawable.tag_screen_select_false);
                tv_tab_two.setTextColor(Color.parseColor("#222222"));
                tv_tab_two.setText("筛选标签");
                tv_tab_one.setBackgroundResource(R.drawable.item_tab_text_true);
                tv_tab_one.setTextColor(Color.parseColor("#ffffff"));
            }
        }

        //设置 popupWindow 里的按钮的事件
        List<String> list = new ArrayList<>();
        list.add("不限");
        list.add("言情");
        list.add("纯爱");
        list.add("衍生");
        tgv_one.requestShowMeasure(true);
        tgv_one.setRadioButtonByFlg(list, R.layout.tag_screen_radiobutton, 0);
        if (StUtils.isEmpty(oneIds)) {
            tgv_one.checkSelecteDefault(0, 0);
        } else {
            tgv_one.checkSelecteDefault(Integer.parseInt(oneIds), 0);
        }

        tgv_one.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == 0) {
                    oneIds = "";
                } else {
                    oneIds = checkedId + "";
                }


            }
        });
        List<String> list2 = new ArrayList<>();
        List<String> list2Ids = new ArrayList<>();
        ((FFActivity) context).post(Url.listChannel, null, ChannelListResponse.class, new FFNetWorkCallBack<ChannelListResponse>() {
            @Override
            public void onSuccess(ChannelListResponse response) {
                List<ChannelListResponse.DataBean> data = response.getData();
                if (response.getCode() == 200 && response.getData().size() > 0) {
                    int channelPos = -1;

                    for (int i = 0; i < data.size(); i++) {
                        list2.add(data.get(i).getName());
                        list2Ids.add(data.get(i).getChannelId());
                        if (!StUtils.isEmpty(twoIds) && twoIds.equals(data.get(i).getChannelId())) {
                            channelPos = i;
                        }
                    }

                    tgv_two.requestShowMeasure(true);
                    tgv_two.setRadioButtonByFlg(list2, R.layout.tag_screen_radiobutton, 1);
                    if (channelPos >= 0) {
                        tgv_two.checkSelecteDefault(channelPos + 1, 1);

                    } else {
                        tv_two.setBackgroundResource(R.drawable.item_tab_text_true);
                        tv_two.setTextColor(Color.parseColor("#ffffff"));
                    }
                }
            }

            @Override
            public boolean onFail(ChannelListResponse response) {
                return false;
            }
        }, "{\"tab\":\"" + "\"}");

        tgv_two.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @SuppressLint("ResourceType")
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId > -1) {
                    twoIds = list2Ids.get(checkedId - 1);
                    tv_two.setBackgroundResource(R.drawable.tag_screen_select_false);
                    tv_two.setTextColor(Color.parseColor("#222222"));
                } else {
                    twoIds = "";
                    tv_two.setBackgroundResource(R.drawable.item_tab_text_true);
                    tv_two.setTextColor(Color.parseColor("#ffffff"));
                }
            }
        });
        tv_two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tgv_two.clearCheck();

            }
        });

        List<String> list3 = new ArrayList<>();
        list3.add("30万以下");
        list3.add("30-50万");
        list3.add("50-100万");
        list3.add("100万以上");
        tgv_three.requestShowMeasure(true);
        tgv_three.setRadioButtonByFlg(list3, R.layout.tag_screen_radiobutton, 1);
        if (!StUtils.isEmpty(threeIds)) {
            tgv_three.checkSelecteDefault(Integer.parseInt(threeIds), 1);
            tv_three.setBackgroundResource(R.drawable.tag_screen_select_false);
            tv_three.setTextColor(Color.parseColor("#222222"));
        }
        tgv_three.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @SuppressLint("ResourceType")
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                if (checkedId > -1) {
                    threeIds = checkedId + "";
                    tv_three.setBackgroundResource(R.drawable.tag_screen_select_false);
                    tv_three.setTextColor(Color.parseColor("#222222"));
                } else {
                    threeIds = "";
                    tv_three.setBackgroundResource(R.drawable.item_tab_text_true);
                    tv_three.setTextColor(Color.parseColor("#ffffff"));
                }
            }
        });
        tv_three.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tgv_three.clearCheck();
            }
        });
        List<String> list4 = new ArrayList<>();
        list4.add("近1年");
        list4.add("近2年");
        list4.add("近3年");
        tgv_four.requestShowMeasure(true);
        tgv_four.setRadioButtonByFlg(list4, R.layout.tag_screen_radiobutton, 1);

        if (!StUtils.isEmpty(fourIds)) {
            if (fourIds.contains("20")) {
                ll_time.setBackgroundResource(R.drawable.item_tab_text_true);
                tv_time.setTextColor(Color.parseColor("#ffffff"));
                tv_four.setBackgroundResource(R.drawable.tag_screen_select_false);
                tv_four.setTextColor(Color.parseColor("#222222"));
                tv_time.setText(fourIds);
            } else {
                tgv_four.checkSelecteDefault(Integer.parseInt(fourIds), 1);
                tv_four.setBackgroundResource(R.drawable.tag_screen_select_false);
                tv_four.setTextColor(Color.parseColor("#222222"));
            }
        }
        ll_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TimeGroupPop.selectTimeGroupPop(mView, context, new TimeGroupPop.PopDismissListener() {
                    @Override
                    public void onOkClick(String time) {
                        if (!StUtils.isEmpty(time)) {
                            tv_time.setText(time);
                            fourIds = time;
                            tgv_four.clearCheck();
                            tv_four.setBackgroundResource(R.drawable.tag_screen_select_false);
                            tv_four.setTextColor(Color.parseColor("#222222"));
                            ll_time.setBackgroundResource(R.drawable.item_tab_text_true);
                            tv_time.setTextColor(Color.parseColor("#ffffff"));
                        }

                    }
                });
            }
        });
        tgv_four.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @SuppressLint("ResourceType")
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                if (checkedId > -1) {
                    fourIds = checkedId + "";
                    tv_four.setBackgroundResource(R.drawable.tag_screen_select_false);
                    tv_four.setTextColor(Color.parseColor("#222222"));
                    ll_time.setBackgroundResource(R.drawable.tag_screen_select);
                    tv_time.setTextColor(Color.parseColor("#222222"));
                } else {
                    if (!fourIds.contains("20")) {
                        fourIds = "";
                        tv_four.setBackgroundResource(R.drawable.item_tab_text_true);
                        tv_four.setTextColor(Color.parseColor("#ffffff"));
                        ll_time.setBackgroundResource(R.drawable.tag_screen_select);
                        tv_time.setTextColor(Color.parseColor("#222222"));
                    }
                }
            }
        });
        tv_four.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fourIds = "";
                tgv_four.clearCheck();
            }
        });
        tv_tab_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tagsId.clear();
                tv_tab_two.setBackgroundResource(R.drawable.tag_screen_select_false);
                tv_tab_two.setTextColor(Color.parseColor("#222222"));
                tv_tab_two.setText("筛选标签");
                tv_tab_one.setBackgroundResource(R.drawable.item_tab_text_true);
                tv_tab_one.setTextColor(Color.parseColor("#ffffff"));
            }
        });
        tv_tab_two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new TabScreenPop().tabScreenPopWindow(mView, context, tagsId, new TabScreenPop.PopTabScreenListener() {
                    @Override
                    public void onValueClick(List<String> value) {
                        if (value == null) {
                            tagsId.clear();
                            tv_tab_two.setBackgroundResource(R.drawable.tag_screen_select_false);
                            tv_tab_two.setTextColor(Color.parseColor("#222222"));
                            tv_tab_two.setText("筛选标签");
                            tv_tab_one.setBackgroundResource(R.drawable.item_tab_text_true);
                            tv_tab_one.setTextColor(Color.parseColor("#ffffff"));
                        } else {
                            tagsId = value;
                            tv_tab_one.setBackgroundResource(R.drawable.tag_screen_select_false);
                            tv_tab_one.setTextColor(Color.parseColor("#222222"));
                            tv_tab_two.setText("已选" + value.size() + "个");
                            tv_tab_two.setBackgroundResource(R.drawable.item_tab_text_true);
                            tv_tab_two.setTextColor(Color.parseColor("#ffffff"));
                        }
                    }
                });
            }
        });

        tv_quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tgv_one.checkSelecteDefault(0, 0);

                tgv_two.clearCheck();
                tv_two.setBackgroundResource(R.drawable.item_tab_text_true);
                tv_two.setTextColor(Color.parseColor("#ffffff"));

                threeIds = "";
                tgv_three.clearCheck();
                tv_three.setBackgroundResource(R.drawable.item_tab_text_true);
                tv_three.setTextColor(Color.parseColor("#ffffff"));

                tgv_four.clearCheck();
                tv_four.setBackgroundResource(R.drawable.item_tab_text_true);
                tv_four.setTextColor(Color.parseColor("#ffffff"));
                tagsId.clear();
                tv_tab_two.setBackgroundResource(R.drawable.tag_screen_select_false);
                tv_tab_two.setTextColor(Color.parseColor("#222222"));
                tv_tab_two.setText("筛选标签");
                tv_tab_one.setBackgroundResource(R.drawable.item_tab_text_true);
                tv_tab_one.setTextColor(Color.parseColor("#ffffff"));
            }
        });

        tv_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                bookRequest.setTab(oneIds);
                if (!StUtils.isEmpty(twoIds)) {
                    List<String> channel = new ArrayList<>();
                    channel.add(twoIds);
                    bookRequest.setChannels(channel);
                } else {
                    bookRequest.setChannels(null);
                }
                bookRequest.setWordSize(threeIds);
                bookRequest.setPublishTime(fourIds);
                if (tagsId != null && tagsId.size() > 0) {
                    bookRequest.setTagIds(tagsId);
                } else {
                    bookRequest.setTagIds(null);
                }
                bookRequest.setOffset(0);
                bookRequest.setLimit(10);
                PreUtils.setString("Interest_Hobby", JSON.toJSONString(bookRequest));
                listener.onValueClick(bookRequest);
                popWindow.dismiss();
            }
        });

    }

    public interface PopInterestListener {
        void onValueClick(ListUserBookRequest value);

    }

    public void setBackgroundAlpha(float alpha, Activity activity) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = alpha;
        activity.getWindow().setAttributes(lp);
    }
}
